package eo.entityManager.locator.impl;

import java.awt.Frame;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.swing.JFrame;

import eo.entityManager.locator.EntityManagerLocator;

public class JFrameBoundEntityManagerLocator implements EntityManagerLocator{

    private Map<Frame, EntityManager> entities = new HashMap<Frame, EntityManager>();
    
    public synchronized void closeEntityManager() {
        Frame active = findActiveFrame();
        EntityManager em = entities.get(active);
        if (em != null && em.isOpen()) {
            em.close();
        }
    }

    private Frame findActiveFrame() {
        Frame active = null;
        Frame[] frames = JFrame.getFrames();
        for (Frame frame : frames) {
            if (frame.isActive()) {
                active = frame;
            }
        }
        return active;
    }

    public synchronized EntityManager findEntityManager() {
        return null;
    }

    public EntityManagerFactory getEntityManagerFactory() {
        return null;
    }

}
